﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace EFCoreDemo.EntityFrameworkCore
{
    public class EFCoreDbContextFactory : IDesignTimeDbContextFactory<EFCoreDbContext>
    {
        public EFCoreDbContext CreateDbContext(string[] args)
        {
            var configuration = BuildConfiguration();
            var builder = new DbContextOptionsBuilder<EFCoreDbContext>()
            .UseSqlServer(configuration.GetConnectionString("ddmMssql"));
            return new EFCoreDbContext(builder.Options);
        }

        private static IConfigurationRoot BuildConfiguration()
        {
            var builder = new ConfigurationBuilder()
           .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), string.Empty))
           .AddJsonFile("appsettings.json", optional: false);

            return builder.Build();
        }
    }
}
